<template>
  <div>
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" :collapse="false" :isSubmitBtn="false"></TopFilter>
    <el-button @click="tempDownHandle" size="small" type="primary">模板下载</el-button>
    <div style="margin-top:20px;"></div>
    <importFile title="导入" :actionUrl="url" :params="params" @close="closeHandler" />
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    ></div>
  </div>
</template>
<script>
import moment from 'moment';
import { getUserId } from '@/assets/js/auth';
import { downloadUrl } from '@/utils';

export default {
  props: ['importData'],

  data() {
    return {
      topFilterList: this.createdFrom(),
      url: `/api/cmscloud.wholesales/ttVsPlanTemplate/upload`,
      params: {
        templateYearAndMonth: moment().format('YYYY-MM'),
        userId: getUserId() ? getUserId() : ''
      }
    };
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'DATE',
          label: '月份',
          fieldName: 'month',
          placeholder: '请选择',
          dateType: 'month',
          clearable: false,
          initialValue: moment().format('YYYY-MM'),
          change: p => {
            this.params.templateYearAndMonth = p;
          }
        }
      ];
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
    },
    tempDownHandle() {
      this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          downloadUrl(this.importData.upUrl, this.importData.name);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    closeHandler() {
      this.$emit('close');
    }
  }
};
</script>
